﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class AttachDA : IAttachDA
    {
        private string ATTACH_ID = "@ATTACH_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string DOC_ID = "@DOC_ID";
        private string DOC_TYPE = "@DOC_TYPE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_ATTACH_ID_UNIQUE = " SELECT COUNT(1) FROM @_@ATTACH WHERE ATTACH_ID = @ATTACH_ID ";
        private string SQL_DELETE_ATTACH = " DELETE FROM @_@ATTACH WHERE ATTACH_ID = @ATTACH_ID ";
        private string SQL_INSERT_ATTACH = " INSERT INTO @_@ATTACH ( ATTACH_ID, DOC_ID, DOC_TYPE, DESCR, STATUS, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @ATTACH_ID, @DOC_ID, @DOC_TYPE, @DESCR, @STATUS, @REMARK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_ATTACH = " SELECT  ROWID, ATTACH_ID, DOC_ID, DOC_TYPE, DESCR, STATUS, REMARK, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@ATTACH ";
        private string SQL_UPDATE_ATTACH = " UPDATE @_@ATTACH SET DOC_ID = @DOC_ID, DOC_TYPE = @DOC_TYPE, DESCR = @DESCR, STATUS = @STATUS, REMARK = @REMARK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE ATTACH_ID = @ATTACH_ID  ";
        private string STATUS = "@STATUS";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";

        public bool CheckAttachIDUnique(string attachID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_ATTACH_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckAttachIDUnique(DataBase dataBase, DbTransaction tran, string attachID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_ATTACH_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteAttach(List<string> attachID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < attachID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ATTACH.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteAttach(DataBase dataBase, DbTransaction tran, List<string> attachID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < attachID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ATTACH.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllAttach(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllAttach(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public AttachInfo GetAttachByID(string attachID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH.Replace("@_@", whLoginID) + " WHERE ATTACH_ID = @ATTACH_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID) };
            AttachInfo attachInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    attachInfo = new AttachInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["ATTACH_ID"].ToString(), reader["DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), reader["DESCR"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return attachInfo;
        }

        public AttachInfo GetAttachByID(DataBase dataBase, DbTransaction tran, string attachID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH.Replace("@_@", whLoginID) + " WHERE ATTACH_ID = @ATTACH_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID) };
            AttachInfo attachInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                attachInfo = new AttachInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["ATTACH_ID"].ToString(), reader["DOC_ID"].ToString(), reader["DOC_TYPE"].ToString(), reader["DESCR"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return attachInfo;
        }

        public DataSet GetAttachByQueryList(List<string> sqlWhere, AttachQueryEntity attachQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ATTACH.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!attachQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, attachQuery.CurrentPage, attachQuery.PageSize, attachQuery.SortField, attachQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetAttachByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, AttachQueryEntity attachQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ATTACH.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, attachQuery.CurrentPage, attachQuery.PageSize, attachQuery.SortField, attachQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertAttach(AttachInfo attachInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_Attach_Parameters(attachInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ATTACH.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertAttach(DataBase dataBase, DbTransaction tran, AttachInfo attachInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_Attach_Parameters(attachInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ATTACH.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Attach_Parameters(AttachInfo attachInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.ATTACH_ID, SqlDbType.VarChar, 50), new SqlParameter(this.DOC_ID, SqlDbType.VarChar, 30), new SqlParameter(this.DOC_TYPE, SqlDbType.VarChar, 30), new SqlParameter(this.DESCR, SqlDbType.VarChar, 300), new SqlParameter(this.STATUS, SqlDbType.VarChar, 30), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            paramArray[0].Value = attachInfo.AttachID;
            paramArray[1].Value = attachInfo.DocID;
            paramArray[2].Value = attachInfo.DocType;
            if (!string.IsNullOrEmpty(attachInfo.Descr))
            {
                paramArray[3].Value = attachInfo.Descr;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            paramArray[4].Value = attachInfo.Status;
            if (!string.IsNullOrEmpty(attachInfo.Remark))
            {
                paramArray[5].Value = attachInfo.Remark;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            paramArray[6].Value = attachInfo.CreatedBy;
            paramArray[7].Value = attachInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateAttach(AttachInfo attachInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_Attach_Parameters(attachInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ATTACH.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateAttach(DataBase dataBase, DbTransaction tran, AttachInfo attachInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_Attach_Parameters(attachInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ATTACH.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

